//非汽车样品开发

$(function () {
//$("#fourSheetInfo").hide();
    //变更相应颜色
    var $dropDown=$("#ctl496749");
    $dropDown.text("请选择相应颜色");
    $dropDown.change(function () {
        //alert('下拉框数据变动,当前值：'+$dropDown.val());

        switch ($dropDown.val()) {
            case "黄色":
                $dropDown.css("background-color","yellow");
                $dropDown.css("color","black");
                break;
            case "红色":
                $dropDown.css("background-color","red");
                $dropDown.css("color","white");
                break;
            case "绿色":
                $dropDown.css("background-color","green");
                $dropDown.css("color","white");
                break;
            default:
                break;
        }

    })

})
//计算交期天数
function countResultDay() {
    //下单日期
    var placeOrderTime=$.MvcSheetUI.GetControlValue("placeOrderTime");
    var placeOrderDate=new Date(placeOrderTime);
    //需求日期
    var requirementsTime=$.MvcSheetUI.GetControlValue("requirementsTime");
    var requireDate=new Date(requirementsTime);

    var resultDate=requireDate-placeOrderDate;

    if(requireDate<placeOrderDate){
        alert("需求日期不能小于下单日期!")
        $.MvcSheetUI.SetControlValue("requirementsTime","");
    }

    var resultDay=resultDate/(1*24*60*60*1000);

    $.MvcSheetUI.SetControlValue("KsDeliverytime",resultDay);

    //设置紧急程度
    if(resultDay<=2){
        $.MvcSheetUI.SetControlValue("urgent","High");
    }else if(resultDay<=4){
        $.MvcSheetUI.SetControlValue("urgent","Normal");
    }else{
        $.MvcSheetUI.SetControlValue("urgent","Low");
    }

}
function itemNos(){
    var itemNo = $.MvcSheetUI.GetControlValue("itemNo");

    $.ajax({
        async:false,
        url:'/Portal/Sample/mappingSampleNo',
        type:'POST',
        data:{
            itemNo:itemNo,
            bizoBjectID:$.MvcSheetUI.SheetInfo.BizObjectID
        },
        dataType:'json',
        success:function(data){
            if(data.status==200){
                var applicationNo=data.data;
                $.MvcSheetUI.SetControlValue("applicationNo",applicationNo);
            }
        }
    })

}

// 表单验证接口
$.MvcSheet.Validate = function () {
    var applicationNo = $.MvcSheetUI.GetControlValue("applicationNo");
    var itemNo = $.MvcSheetUI.GetControlValue("itemNo");
    if($.MvcSheetUI.SheetInfo.IsOriginateMode){
        $.ajax({
            async:false,
            url:'/Portal/Sample/CheckSampleNo',
            type:'POST',
            data:{
                applicationNo:applicationNo,
                itemNo:itemNo
            },
            dataType:'json',
            success:function(data){
                if(data.status==200){
                    var application=data.data;
                    if(application!=applicationNo){
                        $.MvcSheetUI.SetControlValue("applicationNo",application);
                    }
                }
            }
        })
    }
    if($.MvcSheetUI.SheetInfo.ActivityCode == "Activity2"){
        tjqjy();
    }

}
// 页面加载完成事件
$.MvcSheet.Loaded = function (sheetInfo) {
    $("div[data-title='项目编号']").find(".modal-content").css("right","200px");
    var mode = getParam("Mode");
    if(!$.MvcSheetUI.SheetInfo.hasOwnProperty("ActivityCode") && mode=="Work")  {
        $("li[data-action='Save']").hide();
        $("input,textarea").attr("disabled",true)

    }


    $("div[data-title='当前研发单状态']").find("a").text("查看");


    if($.MvcSheetUI.SheetInfo.IsOriginateMode){

        $.MvcSheetUI.SetControlValue("fillPerson",$.MvcSheetUI.SheetInfo.UserID);
        $.MvcSheetUI.SetControlValue("applicant",$.MvcSheetUI.SheetInfo.UserID);

    }
    var address=$.MvcSheetUI.SheetInfo.OriginatorOU;
    if($.MvcSheetUI.SheetInfo.IsOriginateMode){
        $.ajax({
            async:false,
            url:'/Portal/Sample/newSampleNo',
            type:'GET',
            data:{
                address:address
            },
            dataType:'json',
            success:function(data){
                if(data.status==200){
                    var datas =data.data;
                    if(datas!=""||datas!=undefined){
                        $.MvcSheetUI.SetControlValue("applicationNo",datas["applicationNo"]);
                        $.MvcSheetUI.SetControlValue("itemNo",datas["itemNo"]);
                    }
                }
            }
        })
    }

}

function changeNo(){
    var subno = $.MvcSheetUI.GetControlValue("applicationNo");
    subno = subno.substring(subno.length-2,subno.length);
    if(Number(subno)>1){
        $.MvcSheetUI.GetElement("customerName").attr("disabled",true);
        if(!IsMobile){
            $.MvcSheetUI.GetElement("customerName").parent().find("a").hide();
        }
        $.MvcSheetUI.GetElement("customerCode").attr("disabled",true);
    } else{
        $.MvcSheetUI.GetElement("customerName").removeAttr("disabled");
        if(!IsMobile){
            $.MvcSheetUI.GetElement("customerName").parent().find("a").show();
        }
        $.MvcSheetUI.GetElement("customerCode").removeAttr("disabled");
    }
}

function tjqjy(){
    $.MvcSheetUI.SetControlValue("isFzApp",1);
    //非汽车样品申请JS
    //根据紧急程度做相关限制,判断不需要副总审批的条件
    var nowUrgent=$.MvcSheetUI.GetControlValue("urgent");

    //获取当前申请单号
    var applicationNoStr=$.MvcSheetUI.GetControlValue("applicationNo").split("-");
    var applicationNumber=parseInt(applicationNoStr[1]);

    //是否收费
    var chargeStr=$.MvcSheetUI.GetControlValue("charge");

    //样料值
    var sampleMaterialsStr=parseInt($.MvcSheetUI.GetControlValue("sampleMaterials"));

    //100模具所需材料重量
    var ksWeightStr=parseInt($.MvcSheetUI.GetControlValue("KsHundredpartsWeight"));

    if("High"==nowUrgent){
        //紧急程度为高

        if(applicationNumber!=1){
            //非01单

        }else{
//01单
            //汽车场负责人审批
            $.MvcSheetUI.SetControlValue("isFzApp",2);
        }

    }else{
        //紧急程度不为高
        if(applicationNumber!=1){
            //非01单
            if("收费"==chargeStr){
                //当前条件副总无需审批
                $.MvcSheetUI.SetControlValue("isFzApp",0);
            }else{
                //重量小于25
                if(sampleMaterialsStr<=25){
                    //当前条件副总无需审批
                    $.MvcSheetUI.SetControlValue("isFzApp",0);
                }
                //重量在25-100KG的 && 样料小于100模重量
                if((sampleMaterialsStr>=25&&sampleMaterialsStr<=100)&&sampleMaterialsStr<=ksWeightStr){
                    //当前条件副总无需审批
                    $.MvcSheetUI.SetControlValue("isFzApp",0);
                }
            }

        }else{
            //01单
            //汽车场负责人审批
            $.MvcSheetUI.SetControlValue("isFzApp",2);
        }

    }
}

function getParam(paramName) {
    paramValue = "",
        isFound = !1;
    if (this.location.search.indexOf("?") == 0 && this.location.search.indexOf("=") > 1) {
        arrSource = unescape(this.location.search).substring(1, this.location.search.length).split("&"),
            i = 0;
        while (i < arrSource.length && !isFound) arrSource[i].indexOf("=") > 0 && arrSource[i].split("=")[0].toLowerCase() == paramName.toLowerCase() && (paramValue = arrSource[i].split("=")[1], isFound = !0),
            i++
    }
    return paramValue == "" && (paramValue = null),
        paramValue
}

function setSlpData(inid){
    var itemNo= $.MvcSheetUI.GetControlValue("itemNo");
    var mappingNumber= $.MvcSheetUI.GetControlValue("mappingNumber");
    $.ajax({
        async:false,
        url:'/Portal/Sample/requestSampleData',
        type:'Post',
        data:{
            "instanceId":inid,
            "itemNo":itemNo,
            "appNo":mappingNumber
        },
        dataType:'json',
        success:function(result){
            var urls =   sessionStorage.getItem("kipurl");
            if(IsMobile){
                if(urls!=undefined  && urls!="undefined"){
                    urls =   urls.replace("IsMobile=false","IsMobile=true");
                    location.href= urls ;
                }else{
                    location.reload();
                }
            }else{
                if(urls !=undefined    && urls!="undefined"){
                    location.href= urls ;
                }else{
                    location.reload();
                }
            }
        }
    })
}


function changeData(){

    setTimeout(function(){

        var SheetPostValue = $.MvcSheet.GetMvcPostValue( $.MvcSheet.Action_Save);

        $.MvcSheet.PostSheet({
                Command:  $.MvcSheet.Action_Save,
                MvcPostValue: JSON.stringify(SheetPostValue)
            },
            function (data) {
                sessionStorage.setItem("kipurl", undefined);

                sessionStorage.setItem("kipurl", data.Url);
                var instanceId = data.InstanceId;
                setTimeout(function(){
                    if(instanceId !=null && instanceId !=undefined){
                        setSlpData($.MvcSheetUI.SheetInfo.InstanceId);
                    }
                },800);


            });



    },300);

}


function changeYwy(){
    var code = $.MvcSheetUI.GetControlValue("zjcCode");
    var fd =  {"ServiceCode":"ZSYWYSELECT","RunMethod":"exclusiveSalesman","InParams":[{"LogicTypeString":"ShortString","LogicType":14,"ItemName":"customerCode","ItemValue":code }]};
    $.ajax({
        async:false,
        url:'/Portal/BizService/GetMethodReturnList',
        type:'POST',
        data:{
            formData : JSON.stringify(fd),
        },
        dataType:'json',
        success:function(data){
            if(data!=null && data!=undefined && data.Extend!=undefined){
                $.MvcSheetUI.SetControlValue("KsHostssaleman",data.Extend[0].ItemValue[1][0]);
            }
        }
    })


}
function changeSetSimple(){

    var itemNo= $.MvcSheetUI.GetControlValue("itemNo");
    var mappingNumber= $.MvcSheetUI.GetControlValue("mappingNumber");
    $.ajax({
        async:false,
        url:'/Portal/Sample/requestSampleData',
        type:'Post',
        data:{
            "itemNo":itemNo,
            "appNo":mappingNumber
        },
        dataType:'json',
        success:function(result){
            if(result.status="200"){
                if(result.data!=undefined){
                    $.MvcSheetUI.SetControlValue("projectPurpose",result.data.projectPurpose);

                    $.MvcSheetUI.SetControlValue("KsHostssaleman",result.data.KsHostssaleman);
                    $.MvcSheetUI.SetControlValue("ResearchAssociate",result.data.ResearchAssociate);
                    $.MvcSheetUI.SetControlValue("eopTime",result.data.eopTime);
                    $.MvcSheetUI.SetControlValue("sopTime",result.data.sopTime);
                    $.MvcSheetUI.SetControlValue("productsSurface",result.data.productsSurface);
                    $.MvcSheetUI.SetControlValue("backOffice",result.data.backOffice);
                    $.MvcSheetUI.SetControlValue("customerYtTd",result.data.customerYtTd);
                    $.MvcSheetUI.SetControlValue("thickness",result.data.thickness);
                }


            }
        }
    });

}
